package aim;

import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * Constants.java
 * 
 * This class holds several constants used throughout the program.
 * 
 * @author Zachary M. Allen
 */
public class Constants {

	/**
	 * First custom break character
	 */
	public static final int FIRST_CUSTOM_BC = 32;

	/**
	 * Last custom break character
	 */
	public static final int LAST_CUSTOM_BC = 126;
	
	/**
	 * End-of-file character
	 */
	public static final int EOF = -1;
	
	/**
	 * Carriage return character
	 */
	public static final char CR = 13;
	
	/**
	 * Line feed character
	 */
	public static final char LF = 10;

	/**
	 * Number of bits to represent a month
	 */
	public static final int MONTH_BITS = 4;
	
	/**
	 * Number of bits to represent a date 
	 */
	public static final int DATE_BITS = 5;
	
	/**
	 * Number of bits to represent a year
	 */
	public static final int YEAR_BITS = 14;
	
	/**
	 * Number of bits to represent hours
	 */
	public static final int HOUR_BITS = 5;
	
	/**
	 * Number of bits to represent minutes
	 */
	public static final int MINUTE_BITS = 6;
	
	/**
	 * Number of bits to represent seconds
	 */
	public static final int SECOND_BITS = 6;

	/**
	 * Number of bits used per line bitmask
	 */
	public static final int DEFAULT_BITS_PER_BITMASK = 16;

	/**
	 * Number of bits used to represent a file length
	 */
	public static final int FILE_LENGTH_BITS = 8;

	/**
	 * The number of bits in one ASCII character
	 */
	public static final int BITS_PER_ASCII_BYTE = 8;

	/**
	 * Number of bits used to represent total corpus chars
	 */
	public static final int TOTAL_CORPUS_CHARS_BITS = 32;

	/**
	 * Number of bits used to represent total corpus words
	 */
	public static final int TOTAL_CORPUS_WORDS_BITS = 32;

	/**
	 * Number of bits used to represent total corpus lines
	 */
	public static final int TOTAL_CORPUS_LINES_BITS = 32;

	/**
	 * Number of bits representing the number of input files
	 */
	public static final int NUM_INPUT_FILES_BITS = 16;

	/**
	 * Number of bits used to represent the number of bits used per offset
	 */
	public static final int NUM_OFFSET_BITS = 7;

	/**
	 * Number of bits used to represent file chars
	 */
	public static final int FILE_CHARS_BITS = 32;

	/**
	 * Number of bits used to represent file words
	 */
	public static final int FILE_WORDS_BITS = 32;

	/**
	 * Number of bits used to represent file lines
	 */
	public static final int FILE_LINES_BITS = 32;


  /**
   * Number of bits used for the break characters bitmap
   */
  public static final int BREAK_CHARS_BITS = 95;

	/**
	 * Name of output file
	 */
	public static final String OUTPUT_FILE_NAME = "AIM.out";
	
	/**
	 * Checks the next char in an input stream
	 * @param    fis    The input stream to read
	 * @return    Next character in input stream
	 * @throws    IOException
	 */
	public static int peek(BufferedInputStream fis) throws IOException {
		int nextChar = 0;
		fis.mark(32);
	    nextChar = fis.read();
	    fis.reset();
	    return nextChar;
	}

	/**
	 * Resizes a char array when necessary
	 * @param    charArray    Char array to resize
	 * @param    newSize    New size of array
	 */
	public static char[] resizeCharArray(char[] charArray, int newSize) {
		char[] newCharArray = new char[newSize];
		for (int i = 0; i < charArray.length; i++) {
			newCharArray[i] = charArray[i];
		}
		return newCharArray;
	}

	/**
	 * Resizes an int array when necessary
	 * @param    intArray    Int array to resize
	 * @param    newSize    New size of array
	 */
	public static int[] resizeIntArray(int[] intArray, int newSize) {
		int[] newIntArray = new int[newSize];
		for (int i = 0; i < intArray.length; i++) {
			newIntArray[i] = intArray[i];
		}
		return newIntArray;
	}

	/**
	 * Prints a usage error to STDERR
	 *
	 */
	public static void usageError() {
		usageError("There was a problem with your program options;\nplease refer to the following usage instructions:\n\n");
	}

	/**
	 * Prints a usage error to STDERR
	 * @param    error    Error to print
	 */
	public static void usageError(String error) {
		StringBuffer sb = new StringBuffer();
		sb
				.append(error)
				.append("USAGE: ")
				.append("AIM [options] [files]\n\n")
				.append("where [options] is 0 or more of the following:\n")
				.append("-b <string of break characters>\n")
				.append(
						"-s <file name (with path if necessary) of a stop word file>\n")
				.append(
						"\n\nand [files] is 1 or more input files to index");
		System.err.println(sb.toString());
		System.exit(-1);
	}

	/**
	 * This method displays the passed in error
	 * 
	 * @param    error    Error to be displayed
	 */
	public static void errorOccurred(String error) {
		Constants.usageError(error + "\n\n");
	}
	
}